import Vue from "vue";
import Vuex from 'vuex'


const state ={
    is:false,//是否登陆  登陆true
    loginData:{
        // 由于这里的空对象自带属性，用id判断是否有登陆数据
        id:true
    },
    routerArr:[],//静态加动态路由
    currentPlaylist:[],
    currentPaly:''
}
const mutations={
    //点击了登陆路由
    CHANGESTORE(state,bool){
        state.is = bool;
        localStorage.setItem('loginIs',bool)
        state.loginData = bool
    },
    //存放路由
    HEARDER(state,data){
        state.routerArr = data
    },
    setLoginData(state,data){
        state.loginData = data
        var loginDatas = data
        localStorage.setItem('loginDtata',JSON.stringify(loginDatas))
       
    },
    SETCurrentPlaylist: (state, data) => {
        
        // [object Object] 一首歌 [object Array]一个列表
         // 这首歌在当前播放列表是否存在 判断当前播放和新存的是否同一首
         //存在  不存                       同一首   不换
         //不存在  存                       不同     换

        //  如果是string的话就是存入一首歌

        if (Object.prototype.toString.call(data) == '[object String]') {
            state.currentPaly = data
         } else {
            if (Object.prototype.toString.call(data)=='[object Object]') {
                let item = [...state.currentPlaylist].find(item => {
                   return item.id == data.id;
               });
               // 是否存在这首歌
               if(item==undefined){
                   state.currentPlaylist.push(data)
                   state.currentPaly = data.songUrl
               }else {state.currentPaly = data.songUrl}
         } else {
                // 数组则替换歌单
           if (Object.prototype.toString.call(data)=='[object Array]') {
               state.currentPlaylist = data
               state.currentPaly = data[0].songUrl
           } else {
            state.currentPaly = data.songUrl
           }
       }
         }
        //  console.log(state.currentPlaylist)
        //  console.log(state.currentPaly)
        //  if (Object.prototype.toString.call(data)=='[object Object]') {
        //          // 判断是否追加播放 还是替换歌单
        //          console.log('对象')
        //     // 数组则替换歌单
        //     if (Object.prototype.toString.call(data)=='[object Array]') {
        //         state.currentPlaylist = data
        //         state.currentPaly = data[0].songUrl
        //     } else {
        //         if (Object.prototype.toString.call(data)=='[object Object]') {
        //             state.currentPlaylist.push(data)
        //             state.currentPaly = data.songUrl
        //         }else{
        //             state.currentPaly = data.songUrl
        //             return ''
        //         }
        //     }
        //     console.log(state.currentPlaylist)
        //   } else {
        //         return
        // }

  
    },
}
const actions={
    updataStroeChange(context,state){
        context.commit('CHANGESTORE',state)
    },
    //控制存放路由
    Nav(context,state){
        context.commit('HEARDER',state)
    },
    setCurrentPlaylist(context,state){
        context.commit('SETCurrentPlaylist',state)
    },

}
const getters={
    //吐出来当前是退出还是登陆
    updataStore(){
        if(!state.is){
            state.is =  localStorage.getItem('loginIs')
        }
        return state.is
    },
    updateNav(){
        return state.routerArr
    },
    updataLoginData(){
        if(!state.id == true){
            if(typeof(localStorage.getItem('loginDtata') == 'string')){
                state.loginData=JSON.parse(localStorage.getItem('loginDtata'))
               }else{
                state.loginData = localStorage.getItem('loginDtata')
               }
        }

        return state.loginData
    },
    updataCurrentPlaylist(){
        return state.currentPlaylist
    },
    updataCurrentPlay(){
        return state.currentPaly
    }
}
Vue.use(Vuex)
const store = new Vuex.Store({
    state,mutations,actions,getters
})

export default store   